package com.dpteam.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dpteam.bean.*;
import com.dpteam.model.Member;
import com.dpteam.service.ProductService;
import com.dpteam.service.SaleService;
import com.dpteam.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: luolei
 * Date: 2016/1/11
 * Time: 18:26
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private ProductService productService;

    @Resource
    private SaleService saleService;

    private static Logger logger = LoggerFactory.getLogger(GoodsController.class);

    @RequestMapping("/findByISBN")
    @ResponseBody
    public String findByISBN(String goodsNumber) {
        logger.info("findByISBN [goodsNumber:{}]", goodsNumber);
        // 校验
        boolean check = InputCheckUtil.isBarcode(goodsNumber);
        if (!check) {
            logger.warn("条形码[goodsNumber:{}]格式不对", goodsNumber);
            return "error";
        }

        //TODO 参数校验
        ProductInfo productInfo = productService.findProductByBarcode(goodsNumber);
        sellProductInfo sellProductInfo = null;
        if (productInfo != null) {
            sellProductInfo = TransformUtil.fromProductInfo(productInfo);
            logger.info("result:[{}]", sellProductInfo.toString());
            return sellProductInfo.toString();
        } else {
            logger.warn("根据goodsNumber:{}查询不到product", goodsNumber);
            return "error";
        }
    }

    @RequestMapping(value = "check", method = RequestMethod.POST)
    @ResponseBody
    public String check(String checkParam) {
        logger.info("check [checkParam:{}]", checkParam);
        // 校验
        boolean check = StringUtil.isEmpty(checkParam);
        if (check) {
            logger.warn("checkParam为空");
            return "error";
        }

        JSONObject jsonObject = JSONObject.parseObject(checkParam);
        String phone = jsonObject.getString("phone");
        JSONArray jsonArray = jsonObject.getJSONArray("checkParam");
        List<ProductRecord> list = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject object = jsonArray.getJSONObject(i);
            ProductRecord productRecord = new ProductRecord();
            productRecord.setProductId(object.getIntValue("productId"));
            productRecord.setProductNum(object.getIntValue("productNum"));
            list.add(productRecord);
        }
        Member member = null;
        if (phone != null) {
            member = saleService.findMemberByPhone(phone);
        }

        int result = saleService.checkOut(member, list);
        return "true";
    }

    @RequestMapping("preCheck")
    @ResponseBody
    public String preCheck(String checkParam) {
        logger.info("check [checkParam:{}]", checkParam);
        // 校验
        boolean check = StringUtil.isEmpty(checkParam);
        if (check) {
            logger.warn("checkParam为空");
            return "error";
        }

        CheckParam param = new CheckParam();
        JSONObject jsonObject = JSONObject.parseObject(checkParam);
        String phone = jsonObject.getString("phone");
        JSONArray jsonArray = jsonObject.getJSONArray("checkParam");
        List<ProductRecord> list = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject object = jsonArray.getJSONObject(i);
            ProductRecord productRecord = new ProductRecord();
            productRecord.setProductId(object.getIntValue("productId"));
            productRecord.setProductNum(object.getIntValue("productNum"));
            list.add(productRecord);
        }
        Member member = null;
        if (phone != null) {
            member = saleService.findMemberByPhone(phone);
        }
        saleService.preCheck(member, list, param);
        return param.toString();
    }

    @RequestMapping("checkMemberPhone")
    @ResponseBody
    public String checkMemberPhone(String phone) {
        logger.info("checkMemberPhone[phone:{}]", phone);
        // 校验
        boolean check = InputCheckUtil.isPhone(phone);
        if (!check) {
            logger.warn("电话号码[phone:{}]格式不对", phone);
            return "error";
        }

        Member member = saleService.findMemberByPhone(phone);
        if (member == null) {
            logger.info("false");
            return "false";
        } else {
            logger.info("true");
            return "true";
        }
    }

    @RequestMapping("rejectOrderFind")
    public String rejectOrderFind(String orderId, ModelMap map) {
        logger.info("查询订单[orderId:{}]", orderId);
        // 校验
        boolean check = InputCheckUtil.isNumber(orderId);
        if (!check) {
            logger.warn("订单编号[orderId:{}]格式不对", orderId);
            return "forward:/link/goodsReject";
        }

        if (orderId != null) {
            int id = Integer.parseInt(orderId);
            List<OrderItemInfo> orderItemInfos = saleService.findOrderItem(id);
            map.put(Constant.orderItems, orderItemInfos);
            map.put("orderId", orderId);
        }
        return "goodsReject";
    }

    @RequestMapping("findProduct")
    public String findProduct(String name,@RequestParam(defaultValue = "1")int currentPage,@RequestParam(defaultValue = "10") int pageSize, ModelMap map) {
        logger.info("通过商品名查询商品[name:{}]", name);
        if (name == null) {
            return "forward:/link/goodsSearch";
        }
        Page page = PageFactory.getPage(currentPage, pageSize);
        List<ProductInfo> infos = productService.find(name, page);
        map.put(Constant.products, infos);
        map.put(Constant.page, page);
        return "goodsSearch";
    }
}
